package com.jiayang

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.jiayang.base.loadsir.ErrorCallback
import com.jiayang.base.loadsir.LoadingCallback
import com.jiayang.loadsir.core.LoadService
import com.jiayang.loadsir.core.LoadSir
import com.jiayang.mywebview.R
import com.jiayang.mywebview.databinding.FragmentMywebviewBinding
import com.jiayang.webviewprocess.webviewclient.WebViewCallBack
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * @ClassName MyWebViewFragment
 * @Author JIAYANG
 * @Date 2022/4/24 16:01
 * @Email 1354856871@qq.com
 */
const val WEBVIEW_FRAGMENT_URL: String = "url"
const val WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH : String = "can_native_refresh"
class MyWebViewFragment : Fragment(), OnRefreshListener ,WebViewCallBack{
    private var mUrl : String? = null
    private var mCanNativeRefresh = false

    private lateinit var mBinding : FragmentMywebviewBinding
    private lateinit var mLoadService : LoadService<*>

    private var mIsError = false
    private val TAG = "WebViewFragment"

    companion object{
        fun newInstance(url : String?,canNativeRefresh : Boolean) : MyWebViewFragment {
            val fragment = MyWebViewFragment()
            val bundle = Bundle()
            bundle.putString(WEBVIEW_FRAGMENT_URL,url)
            bundle.putBoolean(WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH,canNativeRefresh)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")

//        mBinding.webView.pauseTimers()
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")

//        mBinding.webView.pauseTimers()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = arguments
        if (bundle!=null){
             mUrl = bundle.getString(WEBVIEW_FRAGMENT_URL)
             mCanNativeRefresh = bundle.getBoolean(WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH)
        }

    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_mywebview,container,false)
        mBinding.webView.registerWebViewCallBack(this)
        mBinding.webView.settings.javaScriptEnabled = true
//        mBinding.webView.webViewClient = object : WebViewClient(){
//            private var recentPageFinish = 0L
//            override fun onPageFinished(webview: WebView, url: String) {
//                if (System.currentTimeMillis() - recentPageFinish > 1000) {
////                    parsePage(webview)
////                    mBinding.webView.loadUrl(mUrl!!)
//
//                }
//                recentPageFinish = System.currentTimeMillis()
//            }
//        }
        mBinding.webView.loadUrl(mUrl!!)
        mLoadService = LoadSir.getDefault().register(mBinding.smartRefershLayout){
            mLoadService.showCallback(LoadingCallback::class.java)
            mBinding.webView.reload()
        }
        mBinding.smartRefershLayout.setOnRefreshListener(this)
        mBinding.smartRefershLayout.isEnabled = mCanNativeRefresh

        return mLoadService.loadLayout

    }


    override fun onRefresh(refreshLayout: RefreshLayout) {
        mBinding.webView.reload()
    }

     override fun pageStarted(url: String?) {
         if (mLoadService!=null){
             mLoadService.showCallback(LoadingCallback::class.java)
         }
    }

     override fun pageFinished(view : WebView, url: String?) {
             if (mIsError) {
                 mBinding.smartRefershLayout.isEnabled = true
             } else {
                 mBinding.smartRefershLayout.isEnabled = mCanNativeRefresh
             }
             Log.d(TAG, "pageFinished")
             mBinding.smartRefershLayout.finishRefresh()
             if (mLoadService != null) {
                 if (mIsError) {
                     mLoadService.showCallback(ErrorCallback::class.java)
                 } else {
                     if (mBinding.webView.progress == 100) {
                         mLoadService.showSuccess()
                         mBinding.webView.visibility = View.VISIBLE;
                     }
                 }
             }
    }

     override fun onError() {
         Log.d(TAG,"onError")
         mIsError = true
         mBinding.smartRefershLayout.finishRefresh()

     }

     override fun updateTitle(title: String?) {
         if (activity is MyWebViewActivity){
             (activity as MyWebViewActivity).updateTitle(title)
         }
    }

}